(load "collection.el")

(defun walkthru (k v el)
  (cond ((not (eq (car k) el))
	 (walkthru (cdr k) (cdr v)))
	(else (car v))))
	   
(defun rem2 (l key keys values retkeys retvalues)
  (cond ((nullp l)
	 (error (format "Dictionary : Could not remove %s ! Exiting..." oldObject)));;FIXME do not error
	((eq (car l) key)
	 (setq keys (append retkeys (cdr keys))))
	(t
	 (rem2 (cdr l) key (append (car keys) retkeys) (append (car values) values)))
	))
	  

(defun Dictionary (name)
  (let* ((*class "Dictionary")
	 (*instance-name name)
	 (*keys ())
	 (*values ())
	 (*super (Collection name))
	 
	 (*dispatch
	  (lambda (msg)
	    (cond

	     ;;p169 ST-80
	     ((eq msg "keys")
	      (lambda ()
		*keys))

	     ((eq msg "values")
	      (lambda ()
		*values))

	     ((eq msg "associationAt:")
	      (lambda (key)
		(let ((keys *keys)
		      (values *values))
		  (walkthru keys values key)
		  )))

	     ((eq msg "removeKey:")
	      (lambda (key)
		(rem2 *keys key *keys *values () ())

	     (t
	      ((*super msg)))
	     )))
	 )
    
    *dispatch))
